package com.orange.quickback.demo2;

import com.orange.quickback.system.api.common.result.QBootResult;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Sort;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.LinkedList;
import java.util.List;

@Service
public class QuestServiceImpl {
    @Resource
    QuestRepository questRepository;

    /**
     * 获取所有对象
     * @return 所有对象
     */
    public QBootResult getAllQuests(){
        try {
            List<Quest> questList = questRepository.findAll();
            return QBootResult.build(questList);
        }catch (Exception e){
            return QBootResult.buildFail(e.getMessage());
        }
    }

    /**
     * 通过ID获取对象
     * @param id
     * @return 对象
     */
    public QBootResult getQuestById(Long id){
        try {
            Quest quest = questRepository.findByIdEquals(id);
            return QBootResult.build(quest);
        }catch (Exception e){
            return QBootResult.buildFail(e.getMessage());
        }
    }
    /**
     * 保存或更新一个对象
     */
    public QBootResult saveQuest(Quest quest){
        try {
            return QBootResult.build(questRepository.save(quest));
        }catch (Exception e){
            return QBootResult.buildFail(e.getMessage());
        }
    }
    /**
     * 保存多个对象
     */
    public QBootResult saveQuests(List<Quest> quests){
        try{
            return QBootResult.build(questRepository.saveAll(quests));
        }catch (Exception e){
            return QBootResult.buildFail(e.getMessage());
        }
    }
    /**
     * 分页查询
     */
    public QBootResult getQuestsByPage(int page, int size){
        PageRequest pageRequest = PageRequest.of(page,size);
        try{
            return QBootResult.build(questRepository.findAll(pageRequest));
        }catch (Exception e){
            return QBootResult.buildFail(e.getMessage());
        }
    }
    /**
     * 根据id分页倒序查询
     */
    public QBootResult getQuestsByPageDesc(int page,int size){
        try {
            List<Sort.Order> orders = new LinkedList<>();
            orders.add(new Sort.Order(Sort.Direction.DESC, "id"));
            Sort sort = Sort.by(orders);
            PageRequest pageRequest = PageRequest.of(page,size,sort);
            return QBootResult.build(questRepository.findAll(pageRequest));
        }catch (Exception e){
            return QBootResult.buildFail(e.getMessage());
        }
    }
    /**
     * 根据对象ID删除
     */
    public QBootResult deleteQuestById(Long id){
        try{
            questRepository.deleteById(id);
            return QBootResult.build("删除成功");
        }catch (Exception e){
            return QBootResult.buildFail("删除失败:"+e.getMessage());
        }
    }
    /**
     * 根据对象删除
     */
    public QBootResult deleteQuest(Quest quest){
        try{
            questRepository.delete(quest);
            return QBootResult.build("删除成功");
        }catch (Exception e){
            return QBootResult.buildFail("删除失败:"+e.getMessage());
        }
    }
}
